class BaseChartStyle {
  List<String> color;
  BaseChartLegendStyle legend;
  String backgroundColor;
  BaseChartGridStyle grid;
  bool animation;

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = {
      'animation': animation,
      'color': color,
      'legend': legend?.toJson(),
      'backgroundColor': backgroundColor,
      'grid': grid?.toJson() ?? BaseChartGridStyle().toJson()
    };
    json.removeWhere((key, value) => value == null);
    return json;
  }

  BaseChartStyle(
      {this.color,
      this.legend,
      this.backgroundColor = 'transparent',
      this.grid,
      this.animation = true});
}

/// https://echarts.apache.org/zh/option.html#legend
class BaseChartLegendStyle {
  bool show;
  int zlevel;
  int z;
  int scrollDataIndex;
  double itemGap;
  double itemWidth;
  double itemHeight;
  double borderWidth;
  double borderRadius;
  double shadowBlur;
  double shadowOffsetX;
  double shadowOffsetY;
  String type;
  String formatter;
  String left;
  String top;
  String right;
  String bottom;
  String width;
  String height;
  String orient;
  String align;
  String inactiveColor;
  String backgroundColor;
  String borderColor;
  String shadowColor;
  String selectorPosition;
  BaseChartStylePadding padding;
  BaseChartLegendSelectedMode selectedMode;
  BaseChartLegendTextStyle textStyle;

  BaseChartLegendStyle({
    this.show = true,
    this.zlevel,
    this.z,
    this.scrollDataIndex,
    this.itemGap,
    this.itemWidth,
    this.itemHeight,
    this.borderWidth,
    this.borderRadius,
    this.shadowBlur,
    this.shadowOffsetX,
    this.shadowOffsetY,
    this.type,
    this.formatter,
    this.left,
    this.top,
    this.right,
    this.bottom,
    this.width,
    this.height,
    this.orient,
    this.align,
    this.inactiveColor,
    this.backgroundColor,
    this.borderColor,
    this.shadowColor,
    this.selectorPosition,
    this.padding,
    this.selectedMode,
    this.textStyle,
  });

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = {
      'show': show,
      'zlevel': zlevel,
      'z': z,
      'scrollDataIndex': scrollDataIndex,
      'itemGap': itemGap = 12,
      'itemWidth': itemWidth = 10,
      'itemHeight': itemHeight = 10,
      'borderWidth': borderWidth = 0,
      'borderRadius': borderRadius = 0,
      'shadowBlur': shadowBlur,
      'shadowOffsetX': shadowOffsetX,
      'shadowOffsetY': shadowOffsetY,
      'type': type,
      'formatter': formatter,
      'left': left,
      'top': top,
      'right': right,
      'bottom': bottom,
      'width': width,
      'height': height,
      'orient': orient,
      'align': align,
      'icon': 'circle',
      'inactiveColor': inactiveColor,
      'backgroundColor': backgroundColor,
      'borderColor': borderColor,
      'shadowColor': shadowColor,
      'selectorPosition': selectorPosition,
      'padding': padding?.toJson() ?? BaseChartStylePadding.values([12,0,0,0]).toJson(),
      'selectedMode': selectedMode?.toJson() ??
          BaseChartLegendSelectedMode.setBool(true).toJson(),
      'textStyle': textStyle?.toJson() ?? BaseChartLegendTextStyle().toJson(),
    };
    json.removeWhere((key, value) => value == null);
    return json;
  }
}

/// https://echarts.apache.org/zh/option.html#legend.textStyle
class BaseChartLegendTextStyle {
  int fontSize;
  double lineHeight;
  double width;
  double height;
  double textBorderWidth;
  double textShadowBlur;
  double textShadowOffsetX;
  double textShadowOffsetY;
  double borderWidth;
  double borderRadius;
  double shadowOffsetX;
  double shadowOffsetY;
  String color;
  String fontStyle;
  String fontWeight;
  String fontFamily;
  String borderColor;
  String shadowColor;
  String shadowBlur;
  String backgroundColor;
  String textBorderColor;
  String textShadowColor;
  BaseChartStylePadding padding;

  BaseChartLegendTextStyle({
    this.fontSize = 12,
    this.lineHeight,
    this.width,
    this.height,
    this.textBorderWidth,
    this.textShadowBlur,
    this.textShadowOffsetX,
    this.textShadowOffsetY,
    this.borderWidth,
    this.borderRadius,
    this.shadowOffsetX,
    this.shadowOffsetY,
    this.color,
    this.fontStyle,
    this.fontWeight,
    this.fontFamily,
    this.borderColor,
    this.shadowColor,
    this.shadowBlur,
    this.backgroundColor,
    this.textBorderColor,
    this.textShadowColor,
    this.padding,
  });

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = {
      'fontSize': fontSize,
      'lineHeight': lineHeight,
      'width': width,
      'height': height,
      'textBorderWidth': textBorderWidth,
      'textShadowBlur': textShadowBlur,
      'textShadowOffsetX': textShadowOffsetX,
      'textShadowOffsetY': textShadowOffsetY,
      'borderWidth': borderWidth,
      'borderRadius': borderRadius,
      'shadowOffsetX': shadowOffsetX,
      'shadowOffsetY': shadowOffsetY,
      'color': color,
      'fontStyle': fontStyle,
      'fontWeight': fontWeight,
      'fontFamily': fontFamily,
      'borderColor': borderColor,
      'shadowColor': shadowColor,
      'shadowBlur': shadowBlur,
      'backgroundColor': backgroundColor,
      'textBorderColor': textBorderColor,
      'textShadowColor': textShadowColor,
      'padding': padding?.toJson() ?? BaseChartStylePadding.value(0).toJson()
    };
    json.removeWhere((key, value) => value == null);
    return json;
  }
}

class BaseChartLegendSelectedMode {
  bool _enable;
  String _value;

  BaseChartLegendSelectedMode.setBool(bool enable) : this._enable = enable;

  BaseChartLegendSelectedMode.setString(String value) : this._value = value;

  dynamic toJson() {
    return this._value ?? this._enable ?? true;
  }
}

class BaseChartStylePadding {
  double _value;
  List<double> _values;

  dynamic toJson() {
    return this._values ?? this._value ?? 0;
  }

  BaseChartStylePadding.value(double value) : this._value = value;

  BaseChartStylePadding.values(List<double> values) : this._values = values;
}

class BaseChartGridStyle {
  bool show;
  bool containLabel;
  double borderWidth;
  String left;
  String top;
  String right;
  String bottom;
  String borderColor;

  BaseChartGridStyle({
    this.containLabel = true,
    this.show = false,
    this.left = '0%',
    this.top = '0%',
    this.right = '0%',
    this.bottom = '0%',
    this.borderColor = '#eee',
    this.borderWidth = 1
  });

  Map<String, dynamic> toJson() {
    Map<String,dynamic> json = {
      'containLabel': containLabel,
      'show': show,
      'left': left,
      'top': top,
      'right': right,
      'bottom': bottom,
      'borderColor': borderColor,
      'borderWidth': borderWidth,
    };
    json.removeWhere((key, value) => value == null);
    return json;
  }
}
